package com.mrwang.controller;

import com.mrwang.PageResult;
import com.mrwang.pojo.Sku;
import com.mrwang.pojo.SpuDetail;
import com.mrwang.pojo.SpuVo;
import com.mrwang.service.GoodsService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class GoodsController {
    @Resource
    GoodsService goodsService;
    @GetMapping("spu/page")
    public ResponseEntity<PageResult<SpuVo>> spuVoPage(
            @RequestParam(value = "key",required = false)String key,
            @RequestParam(value = "saleable",required = false) Boolean saleable,
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "5") Integer rows
    ){
        PageResult<SpuVo>  pageList= goodsService.spuVoPage(key,saleable,page,rows);
        if (CollectionUtils.isEmpty(pageList.getItems())){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(pageList);
    }

    @PostMapping("goods")
    public ResponseEntity<Void> saveGoods(@RequestBody SpuVo spuVo){
        goodsService.saveGoods(spuVo);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @GetMapping("spu/detail/{spuId}")
    public ResponseEntity<SpuDetail> spudetailBySpuId(@PathVariable("spuId") Long spuId){
        SpuDetail spuDetail= goodsService.spuDetailBySpuId(spuId);
        return ResponseEntity.ok(spuDetail);
    }

    @GetMapping("sku/list")
    public ResponseEntity<List<Sku>> skuListBySpuId(@RequestParam("id") Long id){
        List<Sku> list= goodsService.skuListBySpuId(id);
        if (CollectionUtils.isEmpty(list)){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(list);
    }

    @PutMapping("goods")
    public ResponseEntity<Void> updateGoods(@RequestBody SpuVo spuVo){
        goodsService.updateGoods(spuVo);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }



}
